package com.stnet.service.enterprice.base;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.stnet.bases.BaseManagerDaoImpl;
import com.stnet.entity.Authorities;
import com.stnet.entity.EnterpriseColumn;
import com.stnet.entity.EnterpriseNews;
import com.stnet.entity.RolesAuthorities;
import com.stnet.front.enterprice.EpColumnOut;
import com.stnet.front.enterprice.IndexInfoBean;

/**
 * @author Messcat
 * @version 1.1
 */
public class EpColumnManagerDaoImpl extends BaseManagerDaoImpl implements EpColumnManagerDao {

	public EpColumnManagerDaoImpl() {
	}

	public List findAllEnterpriseColumn() {
		return this.epColumnDao.findAll();
	}

	public List findByFatherAndState() {
		List epColumnOutList = new ArrayList();
		List epColumnList = epColumnDao.findByFatherAndState(Long.valueOf(0L), "-1", "orderColumn");
		if (epColumnList != null) {
			EpColumnOut epColumnOut;
			for (Iterator iterator = epColumnList.iterator(); iterator.hasNext(); epColumnOutList.add(epColumnOut)) {
				EnterpriseColumn epColumn = (EnterpriseColumn) iterator.next();
				epColumnOut = new EpColumnOut();
				List temp = null;
				temp = epColumnDao.findByFatherAndState(epColumn.getId(), "-1", "orderColumn");
				List threeColumnList = new ArrayList();
				List threeColumn = null;
				for (Iterator iterator1 = temp.iterator(); iterator1.hasNext(); threeColumnList.add(threeColumn)) {
					EnterpriseColumn ep = (EnterpriseColumn) iterator1.next();
					threeColumn = epColumnDao.findByFatherAndState(ep.getId(), "-1", "orderColumn");
				}

				epColumnOut.setEnterpriseColumn(epColumn);
				epColumnOut.setEnterpriseColumnList(temp);
				epColumnOut.setEnterpriseColumnListList(threeColumnList);
			}

		}
		return epColumnOutList;
	}

	public List findEnterpriseColumn() {
		List EpColumnList = epColumnDao.findByFatherAndState(Long.valueOf(0L), "-1", null);
		return EpColumnList;
	}

	public EnterpriseColumn getEnterpriseColumn(Long id) {
		return epColumnDao.get(id);
	}

	/**
	 * 添加栏目时,需要添加相应的权限控制
	 * */
	public void saveEnterpriseColumn(EnterpriseColumn enterpriseColumn) {

		epColumnDao.save(enterpriseColumn);

		Authorities auth = new Authorities();
		auth.setDisplayName(enterpriseColumn.getNames());
		auth.setName(enterpriseColumn.getNames());
		auth.setAuthoritiesType("1");

		this.authoritiesDao.save(auth);
	}

	public void updateEnterpriseColumn(EnterpriseColumn enterpriseColumn, Authorities auth) {

		this.epColumnDao.update(enterpriseColumn);

		this.authoritiesDao.update(auth);

	}

	public void deleteEnterpriseColumn(Long id) {
		List newsList = epNewsDao.findFrontLimitNewsByColumn(1000L, id);
		if (newsList != null) {
			EnterpriseNews en;
			Iterator iterator = newsList.iterator();
			while (iterator.hasNext()) {
				en = (EnterpriseNews) iterator.next();
				epNewsDao.delete(en);
			}
		}
		EnterpriseColumn ec = epColumnDao.get(id);
		Authorities auth = authoritiesDao.getByName(ec.getNames());

		List<RolesAuthorities> list = rolesAuthoritiesDao.findByAuthId(auth.getId());

		if (list != null)
			for (RolesAuthorities ra : list) {
				this.rolesAuthoritiesDao.delete(ra);
			}
		this.authoritiesDao.delete(auth);

		epColumnDao.delete(ec);
	}

	public List findSubColumn(Long father) {
		if (father == -1L)
			return epColumnDao.findAllColumn();
		else
			return epColumnDao.findSubColumn(father);
	}

	public boolean isNameUnique(String names, String orgName, Long father) {
		if (!names.equals(orgName))
			return epColumnDao.isNameUnique(names, father);
		else
			return true;
	}

	public EnterpriseColumn getEnterpriseColumn(String frontNum) {
		return epColumnDao.getColumnByFrontNum(frontNum);
	}

	/**
	 * 核心方法，查找所有标识的栏目的栏目信息和新闻内容信息
	 * */
	public List<IndexInfoBean> findFrontInfoFrontNumNotNull() {
		List indexBean = new ArrayList();
		List tempList = epColumnDao.getColumnByFrontNumNotNull();
		int i = 0;
		if (tempList != null) {
			Iterator iterator = tempList.iterator();
			IndexInfoBean temp = null;
			EnterpriseColumn ec = null;
			StringBuffer SQL = null;
			while (iterator.hasNext()) {
				SQL = null;
				SQL = new StringBuffer(" where 1 = 1 ");
				ec = (EnterpriseColumn) iterator.next();
				temp = new IndexInfoBean();

				/**
				 * 1.link链接栏目则直接设置 2.content单页栏目则需要查找单条新闻
				 * 3.list或者download列表栏目则需要查找栏目新闻
				 * */
				if ("link".equals(ec.getTypeColumn())) {
					temp.setEnterpriseColumn(ec);
				} else if ("content".equals(ec.getTypeColumn())) {
					temp.setEnterpriseColumn(ec);

					SQL.append(" and enterpriseColumn.id = ").append(ec.getId());
					List tempNews = epNewsDao.findNewsByWhere(1L, SQL.toString());

					if (tempNews != null)
						temp.setEnterpriseNews((EnterpriseNews) tempNews.get(0));

					// temp.setEnterpriseColumnList(epColumnDao.findByFatherAndState(ec.getId(),
					// "1", "orderColumn"));

				} else if ("list".equals(ec.getTypeColumn()) || "download".equals(ec.getTypeColumn())
					|| "mostlist".equals(ec.getTypeColumn())) {
					temp.setEnterpriseColumn(ec);

					temp.setEnterpriseColumnList(epColumnDao.findByFatherAndState(ec.getId(), "1", "orderColumn"));

					SQL.append(" and (enterpriseColumn.id = ").append(ec.getId());

					Iterator iter = temp.getEnterpriseColumnList().iterator();

					while (iter.hasNext()) {
						EnterpriseColumn e = (EnterpriseColumn) iter.next();
						SQL.append(" or enterpriseColumn.id = ").append(e.getId());
					}
					SQL.append(")");

					temp.setEnterpriseNewsList(epNewsDao.findNewsByWhere(11L, SQL.toString() + " and state = 1 "));
					/**
					 * 查询5条新闻图片
					 * */
					SQL.append(" and isPrimPhoto = 1 ");
					temp.setEnterprisePhotoNewsList(epNewsDao.findNewsByWhere(5L, SQL.toString() + " and state = 1 "));
				}
				indexBean.add(i, temp);
				i++;
			}
		}
		return indexBean;
	}

	/**
	 * user to the ajax struts the nav tree in behind
	 * 
	 * @param father
	 * @return
	 */
	public List findTreeByFather(Long father) {
		return this.epColumnDao.findAjaxByFatherAndState(father, "1", "orderColumn");
	}

}